package com.xiaochun.jiankangapp.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.xiaochun.jiankangapp.extension.showToast
import com.xiaochun.jiankangapp.model.PersonalInfoModel
import com.xiaochun.jiankangapp.model.Response
import com.xiaochun.jiankangapp.model.UserInfo
import com.xiaochun.jiankangapp.network.Request
import com.xiaochun.jiankangapp.utils.SPUtils
import kotlinx.coroutines.launch

class LoginViewModel : ViewModel() {
    val user: MutableLiveData<UserInfo?> = MutableLiveData()
    val personalInfo = MutableLiveData<PersonalInfoModel?>()
    fun login(name: String?, pass: String?) {
        viewModelScope.launch {
            val r = Request.postSuspend<UserInfo>(
                "api/user/login",
                mapOf("account" to name, "password" to pass)
            )
            user.value = r.data
            if (!r.isSuccessful) {
                showToast("登录失败，" + r.msg)
            }
            SPUtils.setToken(r.data?.userinfo?.token)
        }
    }

    fun reg(phone: String?, pass: String?, code: String?, callback: (Response<UserInfo>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<UserInfo>(
                "api/user/mobilelogin",
                mapOf(
                    "mobile" to phone,
                    "captcha" to code,
                    "password" to pass,
                    "device_number" to SPUtils.getAndroidID()
                )
            )
            if (r.isSuccessful) {
                user.value = r.data
                SPUtils.setToken(r.data?.userinfo?.token)
            }
            callback.invoke(r)
        }
    }

    fun sendCode(phone: String?, type: String? = "register", callback: (Response<Any>) -> Unit) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "api/sms/send",
                mapOf("mobile" to phone, "event" to type)
            )
            callback.invoke(r)
        }
    }

    fun editPass(
        phone: String?,
        pass: String?,
        captcha: String?,
        callback: (Response<Any>) -> Unit
    ) {
        viewModelScope.launch {
            val r = Request.postSuspend<Any>(
                "api/user/resetpwd",
                mapOf("mobile" to phone, "newpassword" to pass, "captcha" to captcha)
            )
            if (r.isSuccessful) {
                val r1 = Request.postSuspend<UserInfo>(
                    "api/user/login",
                    mapOf("account" to phone, "password" to pass)
                )
                user.value = r1.data
                if (!r.isSuccessful) {
                    showToast("登录失败，" + r.msg)
                }
                SPUtils.setToken(r1.data?.userinfo?.token)
                callback.invoke(r)
            } else {
                callback.invoke(r)

            }
        }
    }

    fun refreshPersonInfo() {
        viewModelScope.launch {
            val r = Request.postSuspend<PersonalInfoModel>("api/material/details")
            if (r.isSuccessful) {
                personalInfo.value = r.data
            }
        }
    }

    fun logout() {
        SPUtils.setToken(null)
        user.value = null
        personalInfo.value = null
    }
}